package cn.wolfcode.p2p.controller;

import cn.wolfcode.p2p.base.domain.RealAuth;
import cn.wolfcode.p2p.base.domain.UserInfo;
import cn.wolfcode.p2p.base.exception.DisplayableException;
import cn.wolfcode.p2p.base.qo.BidRequestQueryObject;
import cn.wolfcode.p2p.base.service.IRealAuthService;
import cn.wolfcode.p2p.base.service.IUserInfoService;
import cn.wolfcode.p2p.base.util.Constants;
import cn.wolfcode.p2p.base.util.JSONResult;
import cn.wolfcode.p2p.business.domain.BidRequest;
import cn.wolfcode.p2p.business.service.IBidRequestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.Date;

/**
 * 后台的借款界面
 */
@Controller
public class BidRequestController {

    @Autowired
    private IBidRequestService bidRequestService;
    @Autowired
    private IUserInfoService userInfoService;
    @Autowired
    private IRealAuthService realAuthService;

    @RequestMapping("/bidrequest_publishaudit_list")
    public String publishAuditPage(@ModelAttribute("qo") BidRequestQueryObject qo, Model model){
        qo.setBidRequestState(Constants.BIDREQUEST_STATE_APPLY);
        model.addAttribute("pageInfo", bidRequestService.queryPage(qo));
        return "bidrequest/publish_audit";
    }
    @RequestMapping("/borrow_info")
    public String borrowInfoPage(Long id, Model model){
        BidRequest bidRequest = bidRequestService.get(id);
        String returnPage = "";
        if (bidRequest != null) {
            //bidRequest
            model.addAttribute("bidRequest", bidRequest);
            //audits 空的
            model.addAttribute("audits", new ArrayList());

            if (bidRequest.getBidRequestType() == Constants.BIDREQUEST_TYPE_NORMAL) {
                //userInfo
                UserInfo userInfo = userInfoService.get(bidRequest.getCreateUser().getId());
                model.addAttribute("userInfo", userInfo);
                //realAuth
                RealAuth realAuth = realAuthService.get(userInfo.getRealAuthId());
                model.addAttribute("realAuth", realAuth);
                returnPage = "bidrequest/borrow_info";
            } else if (bidRequest.getBidRequestType() == Constants.BIDREQUEST_TYPE_EXP){
                returnPage = "expbidrequest/borrow_info";
            }
        }
        return returnPage;
    }


    @RequestMapping("/bidrequest_publishaudit")
    @ResponseBody
    public JSONResult publishAudit(Long id, Integer state, @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm") Date publishTime, String remark){
        JSONResult result = null;
        try {
            bidRequestService.audit(id, state, publishTime, remark);
            result = new JSONResult("");
        } catch (DisplayableException ex) {
            //异常需要在前台显示
            ex.printStackTrace();
            result = new JSONResult(false, ex.getMessage());
        } catch (Exception e){
            //异常记录到数据库或日志文件中
            e.printStackTrace();
            result = new JSONResult(false, "系统异常,请联系管理员");
        }
        return result;
    }

    @RequestMapping("/bidrequest_audit1_list")
    public String audit1Page(@ModelAttribute("qo") BidRequestQueryObject qo, Model model){
        qo.setBidRequestState(Constants.BIDREQUEST_STATE_APPROVE_PENDING_1);
        //pageInfo
        model.addAttribute("pageInfo", bidRequestService.queryPage(qo));
        return "bidrequest/audit1";
    }

    @RequestMapping("/bidrequest_audit1")
    @ResponseBody
    public JSONResult audit1(Long id, Integer state, String remark){
        JSONResult result = null;
        try {
            bidRequestService.audit1(id, state, remark);
            result = new JSONResult("");
        } catch (DisplayableException ex) {
            //异常需要在前台显示
            ex.printStackTrace();
            result = new JSONResult(false, ex.getMessage());
        } catch (Exception e){
            //异常记录到数据库或日志文件中
            e.printStackTrace();
            result = new JSONResult(false, "系统异常,请联系管理员");
        }
        return result;
    }

    @RequestMapping("/bidrequest_audit2_list")
    public String audit2Page(@ModelAttribute("qo") BidRequestQueryObject qo, Model model){
        qo.setBidRequestState(Constants.BIDREQUEST_STATE_APPROVE_PENDING_2);
        //pageInfo
        model.addAttribute("pageInfo", bidRequestService.queryPage(qo));
        return "bidrequest/audit2";
    }

    @RequestMapping("/bidrequest_audit2")
    @ResponseBody
    public JSONResult audit2(Long id, Integer state, String remark){
        JSONResult result = null;
        try {
            bidRequestService.audit2(id, state, remark);
            result = new JSONResult("");
        } catch (DisplayableException ex) {
            //异常需要在前台显示
            ex.printStackTrace();
            result = new JSONResult(false, ex.getMessage());
        } catch (Exception e){
            //异常记录到数据库或日志文件中
            e.printStackTrace();
            result = new JSONResult(false, "系统异常,请联系管理员");
        }
        return result;
    }
}
